package com.seari.client;

import com.seari.bean.AllFullLoadBean;
import com.seari.service.AppCacheService;
import com.seari.service.FullLoadService;
import com.seari.service.TrainService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @ClassName FullLoadClient
 * @Description 全网区间满载率
 * @Author LANDSUS
 * @Date 2021/5/16 10:35
 * @Version 1.0
 */
@Component("FullLoadClient")
public class FullLoadClient extends BaseClient{

    protected static Logger log = LoggerFactory.getLogger(FullLoadClient.class);

    @Autowired
    private FullLoadService fullLoadService;

    @Autowired
    private AppCacheService service;
    
    @Autowired
    private TrainService trainService;

    @Override
    public void process() throws Exception {
        List<AllFullLoadBean> data = null;
        try{
            data = fullLoadService.allGroupFullLoad();
        }catch (RuntimeException e){
            log.error(e.getMessage());
        }
        if(data != null && data.size() > 0){
            log.info("allFullLoad get successfully");
            service.refreshCache("allGroupFullLoad",data);
            log.info("allFullLoad save cache successfully");
            try {
                Map<String, Map<String, Integer>> trainFullLoad = trainService.convertFullLoadDataToTrainInfo(data);
                log.info("trainFullLoad convert successfully");
                service.refreshCache("trainFullLoad", trainFullLoad);
                log.info("trainFullLoad save cache successfully");
            }catch (Exception e){
                e.printStackTrace();
                log.error("trainFullLoad convert failed,doesn't save cache");
            }
        }else {
            log.error("allFullLoad get failed,doesn't save cache");
        }
    }
}
